package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Answer;
import cn.wolfcode.luowowo.article.domain.AnswerContent;
import cn.wolfcode.luowowo.article.mapper.AnswerContentMapper;
import cn.wolfcode.luowowo.article.mapper.AnswerMapper;
import cn.wolfcode.luowowo.article.query.AnswerQuery;
import cn.wolfcode.luowowo.article.service.IAnswerService;
import cn.wolfcode.luowowo.article.vo.AnswerSatisSortVo;
import cn.wolfcode.luowowo.cache.service.IAnswerRedisService;
import cn.wolfcode.luowowo.cache.vo.AnswerStatisVO;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author 名字
 * @company 公司
 * @create 2019-09-26 9:43
 */
@Service
public class AnswerServiceImpl implements IAnswerService {
    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private AnswerContentMapper contentMapper;

    @Reference
    private IUserService userService;

    @Reference
    private IAnswerRedisService answerRedisService;

    @Reference
    private IAnswerService answerService;

    @Override
    public Answer getTop1(Long qid) {
        return answerMapper.selectTop1ByQId(qid);
    }

    @Override
    public AnswerContent getContent(Long id) {

        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Answer answer) {
        if (answer.getId() != null) {
            //修改
            answerMapper.updateByPrimaryKey(answer);
            //获取内容
            AnswerContent content = answer.getContent();
            content.setId(answer.getId());
            contentMapper.updateByPrimaryKey(content);

        } else {
            //创建时间
            answer.setCreateTime(new Date());
            //添加
            answerMapper.insert(answer);

            AnswerContent content = answer.getContent();
            content.setId(answer.getId());
            contentMapper.insert(content);
        }
    }

    @Override
    public List<Answer> queryByQId(Long id) {
        List<Answer> answers = answerMapper.selectByQId(id);
        for (Answer answer : answers) {
            AnswerContent content = answerService.getContent(answer.getId());
            if (content != null) {
                answer.setContent(content);
            }
            AnswerStatisVO statisVo = answerRedisService.getStatisVo(answer.getId());
            answer.setThumsupnum(statisVo.getThumbsupnum());
        }
        return answers;
    }

    @Override
    public Answer get(Long id) {

        return answerMapper.selectByPrimaryKey(id);
    }

    @Override
    public Long getAnswerNumByUId(Long uId) {

        return answerMapper.selectAnswerNumByUId(uId);
    }

    @Override
    public Long getGoldNumByUId(Long uId) {
        return answerMapper.GoldNumByUId(uId);
    }

    @Override
    public List<AnswerSatisSortVo> querySort(AnswerQuery answerQuery) {
        List<AnswerSatisSortVo> list = answerMapper.selectTop(answerQuery);
        for (AnswerSatisSortVo answerSatisSortVo : list) {
            UserInfo userInfo = userService.get(answerSatisSortVo.getAuthorId().getId());
            answerSatisSortVo.setAuthorId(userInfo);
        }
        return list;
    }

    @Override
    public void updateThump(Long aid) {
        answerMapper.updateThump(aid);
    }
}
